package com.hue.gulimailseckill.controller;

import com.hue.common.utils.R;
import com.hue.gulimailseckill.service.SeckillService;
import com.hue.gulimailseckill.vo.SeckillRedisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 姚凤桥
 * @Date 2022/1/20 9:37
 * @Description:
 */
@RestController
@RequestMapping("/")
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    /**
     * 查询已经开始的秒杀活动
     *
     * @return
     */
    @GetMapping("/seckill/getSeckillProduce")
    public R getSeckillProduce() {
        List<SeckillRedisVo> produces = seckillService.getSeckillProduce();
        return R.ok().put("produces", produces);
    }

    /**
     * 查询sku的秒杀信息
     *
     * @param skuId
     * @return
     */
    @GetMapping("/skuInfo/seckill/{skuId}")
    public R skuInfoSeckill(@PathVariable("skuId") Long skuId) {
        SeckillRedisVo seckillRedisVo = seckillService.skuInfoSeckill(skuId);
        return R.ok().put("data", seckillRedisVo);
    }

    /**
     * 秒杀商品，如果成功返回订单号
     *
     * @param key
     * @param code
     * @return
     */
    @GetMapping("/kill")
    public R kill(@RequestParam("key") String key, @RequestParam("code") String code) {
        long s1 = System.currentTimeMillis();
        String orderSn = seckillService.kill(key, code);
        long s2 = System.currentTimeMillis();
        System.out.println("秒杀耗时:" + (s2 - s1) + "ms");
        return R.ok().put("msg", orderSn);
    }
}
